package com.zcx.service;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.zcx.util.HttpUtil.GET;
import static com.zcx.util.HttpUtil.sendFileReq;


public class FileService {
    private static final String BASE_URL = "http://127.0.0.1:8888/file/";
    public String uploadFile(File file){
        String result = "";
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setContentType(ContentType.MULTIPART_FORM_DATA);
        builder.addBinaryBody("file",file);
        ContentType contentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), StandardCharsets.UTF_8);
        StringBody stringBody = new StringBody("发送文件",contentType);
        try {
            result = sendFileReq (BASE_URL+"uploadFile", builder, "utf-8");

        } catch (Exception e) {
            e.printStackTrace ( );
        }

        return result;
    }
    public String downloadFile(String uuid){
        Map<String, String> param = new HashMap<>();
        param.put("uuid",uuid);
        return GET(BASE_URL + "downloadFile", param);
    }
    public String getInfoByUUID(String uuid){
        Map<String, String> param = new HashMap<>();
        param.put("uuid",uuid);
        return GET(BASE_URL + "getInfoByUUID", param);
    }
}
